/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MachineManagement.Components;

import org.virtualbox_3_2.NetworkAttachmentType;

/**
 * This class represents the live networking settings of a virtual machine.
 *
 * @author Angel Sanadinov
 */
public class NetworkAdapterLiveSettings extends MachineManagementComponent
{
    private NetworkAttachmentType attachmentType; //type of network attachment
    private String attachedNetworkName;           //name of attached network
    private boolean isCableConnected;             //cable state
    private int lineSpeed;                        //line speed (in units of 1kbps)
    private NATEngineSettings natEngineSetup;     //NAT engine settings
    private boolean adapterModified;              //denotes whether the adapter data should be modified

    /**
     * Constructs a network adapter live settings object with the specified data.
     *
     * @param attachmentType the type of network attachment
     * @param attachedNetworkName the name of the attached network
     * @param isCableConnected the state of the adapter cable
     * @param lineSpeed the line speed of the adapter (in units of 1kbps)
     */
    public NetworkAdapterLiveSettings(NetworkAttachmentType attachmentType, String attachedNetworkName,
                                      boolean isCableConnected, int lineSpeed)
    {
        this.attachmentType = attachmentType;
        this.attachedNetworkName = attachedNetworkName;
        this.isCableConnected = isCableConnected;
        this.lineSpeed = lineSpeed;
        this.natEngineSetup = null;
        this.adapterModified = true;
    }

    /**
     * Constructs a network adapter live settings object for adapters that should
     * not be modified/updated by a request.
     */
    public NetworkAdapterLiveSettings()
    {
        this.adapterModified = false;
    }

    /**
     * Retrieves the network attachment type.
     *
     * @return the attachment type
     */
    public NetworkAttachmentType getAttachmentType()
    {
        return attachmentType;
    }

    /**
     * Retrieves the name of the attached network.
     *
     * @return the network name
     */
    public String getAttachedNetworkName()
    {
        return attachedNetworkName;
    }

    /**
     * Retrieves the state of the adapter's cable.
     *
     * @return <code>true</code> if the cable is connected or <code>false</code>
     *         if it is not
     */
    public boolean getIsCableConnected()
    {
        return isCableConnected;
    }

    /**
     * Retrieves the line speed of the adapter.
     *
     * @return the line speed (in units of 1kbps)
     */
    public int getLineSpeed()
    {
        return lineSpeed;
    }

    /**
     * Retrieves the NAT engine's setup of the adapter.
     *
     * @return the engine setup, if available or <code>null</code> otherwise
     */
    public NATEngineSettings getNatEngineSetup()
    {
        return natEngineSetup;
    }

    /**
     * Returns whether the network adapter represented by this object should be
     * updated/modified.
     *
     * @return <code>true</code> if the adapter should be updated/modified or
     *         <code>false</code> if it shouldn't
     */
    public boolean isAdapterModified()
    {
        return adapterModified;
    }

    /**
     * Sets the NAT engine of the adapter, if necessary.
     *
     * @param natEngineSetup the NAT engine settings
     */
    public void setNatEngineSetup(NATEngineSettings natEngineSetup)
    {
        this.natEngineSetup = natEngineSetup;
    }

    @Override
    public boolean isValid()
    {
        if(!adapterModified || (attachmentType != null && lineSpeed >= 0
                && (natEngineSetup == null || natEngineSetup.isValid())))
            return true;
        else
            return false;
    }
}
